<?php
/**
 * @file
 */
/**
 * Form builder; the edit slider form.
 */
function flexslider_block_edit($form, &$form_state = array(), $slider = NULL) {
  if (is_numeric($slider)) {
    $slider = flexslider_block_image_load($slider);
  }
  $form = array();

  // Start with the default user account fields.
  // Account information.
  $form['image'] = array(
    '#type'   => 'fieldset',
    '#title' => t('Edit image'),
    '#tree' => TRUE
  );
  $form['image']['fbid'] = array(
    '#type' => 'hidden',
    '#value' => !empty($slider->fbid) ? $slider->fbid : 0,
  );
  $form['image']['delta'] = array(
    '#type' => 'hidden',
    '#value' => !empty($slider->delta) ? $slider->delta : '',
  );
  $form['image']['fid']['fid'] = array(
    '#type' => 'hidden',
    '#value' => !empty($slider->fid) ? $slider->fid : '',
  );
  $form['image']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Tilte'),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => !empty($slider->title) ? $slider->title : '',
  );
  $form['image']['url'] = array(
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#maxlength' => 255,
    '#attributes' => array('class' => array('slidergame-slider-url')),
    '#default_value' => !empty($slider->url) ? $slider->url : '',
  );
  $form['image']['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#attributes' => array('class' => array('slidergame-slider-description')),
    '#default_value' => !empty($slider->description) ? $slider->description : '',
  );

  $form['image']['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Active'),
    '#default_value' => !empty($slider->status) ? $slider->status : '',
    '#attributes' => array('class' => array('slidergame-slider-status')),
  );

  $form['image']['actions'] = array('#type' => 'actions');

  $form['image']['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#submit' => array('flexslider_block_edit_submit'),
  );

  $des = drupal_get_destination();

  if (!empty($des)) {
    $form['image']['actions']['cancel'] = array(
      '#type' => 'link',
      '#href' => $des['destination'],
      '#title' => t('Cancel')
    );
  }

  $form['#submit'][] = 'flexslider_block_edit_submit';

  return $form;
}

/**
 * Submit handler for the client form.
 */
function flexslider_block_edit_submit($form, &$form_state) {
  // Remove unneeded values.
  form_state_values_clean($form_state);

  $slider = (object)$form_state['values']['image'];
  $slider = flexslider_block_save_image($slider);
  drupal_set_message(t('The image has been saved successful'));
}